﻿/*
 * User: Carlos
 * Date: 21/01/2007
 * Time: 11:04 a.m.
 * 
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ByteFX.Data.MySqlClient;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Web.Security;

namespace eekbalam.Net
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
    public class Default : MasterPage
    {
        #region Member variables
        /// <summary>
        /// Web Site member Variables
        /// </summary>
        public TreeView TreeView1;
        public Label lblAdminTools;
        public Calendar calContents;
        private String sErrorMessage;

        /// <summary>
        /// DB Connection variables
        /// TODO: Read'em from site.config
        /// </summary>
        private String sConnectionString;
        private MySqlConnection dbcon;
        private MySqlCommand dbcmd;
        private MySqlDataReader objReader;
        private MySqlDataReader rdrTopics;
        private String sSQL;

        #endregion
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region Page Init & Exit (Open/Close DB connections here...)
		protected void PageInit(object sender, EventArgs e)
		{
            
		}
		//----------------------------------------------------------------------
		protected void PageExit(object sender, EventArgs e)
		{
		}
		#endregion
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region Page Load
		private void Page_Load(object sender, EventArgs e)
		{
            /// <summary>
            /// WireUP events
            /// </summary>
            /// 

            /// <summary>
            /// Apply styles
            /// </summary>
            /// 
            applyCalendarStyles();
            if (!IsPostBack)
            {
                sErrorMessage = string.Empty;
                sConnectionString = ConfigurationSettings.AppSettings["SqlConnectionString"].ToString();
                try
                {
                    dbcon = new MySqlConnection(sConnectionString);
                }
                catch (MySqlException ex)
                {
                    switch (ex.Number)
                    {
                        case 0:
                            sErrorMessage += ConfigurationSettings.AppSettings["sUnableToConnect"].ToString();
                            break;
                        case 1044:
                            sErrorMessage += ConfigurationSettings.AppSettings["sAccessDeniedToDB"].ToString();
                            break;
                        case 1045:
                            sErrorMessage += ConfigurationSettings.AppSettings["sAccessDenied"].ToString();
                            break;
                        default:
                            sErrorMessage += string.Format("{0}[{1}]", ex.Message, ex.Number);
                            break;
                    }
                }
                finally
                {
                    dbcon.Open();
                    loadLinkMenu();
                }
            }
		}
		#endregion
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        #region Page Events
        #endregion
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        #region Initialize Component

        protected override void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}
		//----------------------------------------------------------------------
		private void InitializeComponent()
		{
			this.Load	+= new System.EventHandler(Page_Load);
			this.Init   += new System.EventHandler(PageInit);
			this.Unload += new System.EventHandler(PageExit);
		}
		#endregion
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        #region Member Functions
        private void applyCalendarStyles()
        {
            calContents.DayNameFormat = System.Web.UI.WebControls.DayNameFormat.FirstLetter;
            calContents.TitleFormat = (System.Web.UI.WebControls.TitleFormat)1;
            calContents.DayStyle.Font.Size = calContents.DayHeaderStyle.Font.Size = new FontUnit(9);
            calContents.TitleStyle.Font.Size = new FontUnit(10);
            calContents.TitleStyle.BackColor = System.Drawing.Color.FromArgb(0, 0, 128);
            calContents.TitleStyle.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
            calContents.DayStyle.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            calContents.NextPrevFormat = (System.Web.UI.WebControls.NextPrevFormat)1;
            calContents.NextPrevStyle.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
            calContents.SelectedDate = new DateTime(); 
            calContents.SelectedDayStyle.BackColor = System.Drawing.Color.FromArgb(0, 0, 128);
            calContents.SelectedDayStyle.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
        }
        private void loadLinkMenu()
        {
            sSQL = string.Format("SELECT * FROM {0}", ConfigurationSettings.AppSettings["SqlMenuCategoriesTable"].ToString());
            try
            {
                dbcmd = new MySqlCommand(sSQL, dbcon);
                objReader = dbcmd.ExecuteReader();
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 1064:
                        sErrorMessage += ConfigurationSettings.AppSettings["sSQLSyntaxError"].ToString();
                        break;
                    case 1146:
                        sErrorMessage += "Create table";
                        sSQL = string.Format("CREATE TABLE {0} {1}",
                                ConfigurationSettings.AppSettings["SqlMenuCategoriesTable"].ToString(),
                                ConfigurationSettings.AppSettings["sMenuCategoriesSchema"].ToString());
                        try
                        {
                            dbcmd = new MySqlCommand(sSQL, dbcon);
                            dbcmd.ExecuteNonQuery();
                        }
                        catch (MySqlException exCreate)
                        {
                            switch (exCreate.Number)
                            {
                                case 1064:
                                    sErrorMessage += ConfigurationSettings.AppSettings["sSQLSyntaxError"].ToString();
                                    break;
                                default:
                                    sErrorMessage += string.Format("{0}[{1}]", exCreate.Message, exCreate.Number);
                                    break;
                            }
                        }
                        finally
                        {
                        }
                        break;
                    default:
                        sErrorMessage += string.Format("{0}[{1}]", ex.Message, ex.Number);
                        break;
                }
            }
            finally
            {                    
            }
        }
        #endregion
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        #region TreeView functions
        public void PopulateNode(Object source, TreeNodeEventArgs e)
        {
            switch (e.Node.Depth)
            {
                case 0:
                    GetCategories(e.Node);
                    break;
                case 1:
                    GetTopics(e.Node);
                    break;
                default:
                    break;
            }
        }
        private void getCategoryTopics(String catId)
        {
            sSQL = string.Format("SELECT * FROM {0} WHERE categoryId = {1}", ConfigurationSettings.AppSettings["SqlMenuTopicsTable"].ToString(), catId);
            try
            {
                dbcmd = new MySqlCommand(sSQL, dbcon);
                rdrTopics = dbcmd.ExecuteReader();
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 1064:
                        sErrorMessage += ConfigurationSettings.AppSettings["sSQLSyntaxError"].ToString();
                        break;
                    case 1146:
                        sErrorMessage += "Create table";
                        sSQL = string.Format("CREATE TABLE {0} {1}",
                                ConfigurationSettings.AppSettings["SqlMenuTopicsTable"].ToString(),
                                ConfigurationSettings.AppSettings["sMenuTopicsSchema"].ToString());
                        try
                        {
                            dbcmd = new MySqlCommand(sSQL, dbcon);
                            dbcmd.ExecuteNonQuery();
                        }
                        catch (MySqlException exCreate)
                        {
                            switch (exCreate.Number)
                            {
                                case 1064:
                                    sErrorMessage += ConfigurationSettings.AppSettings["sSQLSyntaxError"].ToString();
                                    break;
                                default:
                                    sErrorMessage += string.Format("{0}[{1}]", exCreate.Message, exCreate.Number);
                                    break;
                            }
                        }
                        finally
                        {
                        }
                        break;
                    default:
                        sErrorMessage += string.Format("{0}[{1}]", ex.Message, ex.Number);
                        break;
                }
            }
            finally
            {
            }
        }
        private void GetTopics(TreeNode node)
        {
            getCategoryTopics(node.Value);
            while (rdrTopics.Read())
            {
                string topicName = (string)rdrTopics["TopicName"];
                TreeNode newNode = new TreeNode(topicName);
                newNode.PopulateOnDemand = false;
                newNode.SelectAction = TreeNodeSelectAction.None;
                newNode.NavigateUrl = (string)rdrTopics["topicUrl"];
                node.ChildNodes.Add(newNode);
            }
            rdrTopics.Close();
        }
        private void GetCategories(TreeNode node)
        {
            while (objReader.Read())
            {
                string categoryName = (string)objReader["categoryName"];
                string categoryValue = (string)objReader["categoryValue"];
                TreeNode newNode = new TreeNode(categoryName, categoryValue);
                newNode.PopulateOnDemand = true;
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(newNode);
            }
            objReader.Close();
        }
       #endregion
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    }
}